//           loginwindow.cc
//  Fri Jan  5 14:07:41 2007
//  Copyright  2007  Min Seok Seo
//  <msseo@oasyss.co.kr>

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with main.c; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA

#include "loginwindow.h"
#include <iostream>
#include <gtkmm.h>

using namespace PGSTD;
using namespace pqxx;
using namespace std;

LoginWindow::LoginWindow()
{	
	// 로그인 체크 초기화
	//fullscreen();
	Sales sales;
	strConn = sales.strConn;
	//strConn = "host=127.0.0.1 port=5432 dbname=root user=root";
	login_check = false;
	login_user = "x";
	login_sosok = "x";
		
	// RC 파일 설정
	Gtk::RC("./rc/loginwindowrc");
	// 윈도우 설정
  	set_title ("리눅스 Point Of Sales");
  	set_border_width(0);
  	set_default_size(1024,768);

	// 글래이드 파일 불러오기
	m_refGlade = Gnome::Glade::Xml::create("./glade/login.glade");
	m_refGlade->reparent_widget("vbox", *this);	
	m_pLoginWindowMap = new Gnome::Glade::VariablesMap(m_refGlade);
	
	// 메인 윈도우 기본 버튼 이벤트 설정 (아이디 패스워드 엔트리, 확인 취소 버튼, 상태 정보창)
	m_pLoginWindowMap->connect_widget("Entry_id", m_Entry_id);
	m_pLoginWindowMap->connect_widget("Entry_pw", m_Entry_pw);
	m_refGlade->connect_clicked("Button_Login_accept", sigc::mem_fun(*this, &LoginWindow::on_Button_Login_accept_clicked));
	m_refGlade->connect_clicked("Button_Login_cancel", sigc::mem_fun(*this, &LoginWindow::on_Button_Login_cancel_clicked));
	m_refGlade->get_widget("statusbar_login",m_Statusbar_login);  	
	m_refGlade->get_widget("loginframe",pLoginFrame);
	pImage = Gtk::manage(new Gtk::Image("./glade/logo01.png"));
	m_Statusbar_login->push("Linux POS 로그인");
	pLoginFrame->add(*pImage);
	show_all_children();
}

LoginWindow::~LoginWindow()
{
	if(m_pLoginWindowMap)
	{
		delete m_pLoginWindowMap;
		m_pLoginWindowMap = 0;
	}
}

void LoginWindow::on_Button_Login_accept_clicked()
{	
  	if(m_pLoginWindowMap)
    	m_pLoginWindowMap->transfer_widgets_to_variables();
    
	m_Statusbar_login->push("데이터베이스 접속중 ...");
	connection C(strConn);
	m_Statusbar_login->push("데이터베이스 접속완료");
	//m_Statusbar_login->push("데이터베이스 접속 실패");
	
	std::string SQL;
	SQL = 
	"SELECT \
		OPER_USER_SOSOK, \
		OPER_USER_ID \
	FROM \
		TB_OPER_USER \
	WHERE \
		OPER_USER_ID = '"+ m_Entry_id +"'\
		AND OPER_USER_PASS = '"+ m_Entry_pw + "'";
		
	work T( C, "login" );
	result R( T.exec(SQL) );
	result::const_iterator c = R.begin();
	if(R.empty())
		m_Statusbar_login->push("사용자 인증 : 아이디나 패스워드가 잘못 되었습니다.");
	else {
		login_check = true;	
		login_sosok = c[0].as(string());
		login_user = c[1].as(string());
		hide();
	}
	T.commit();
	C.disconnect();
}

void LoginWindow::on_Button_Login_cancel_clicked()
{
	login_check = false;
	hide();
}
